package com.cgtong.android.activity;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.cgtong.android.R;
import com.cgtong.android.base.BaseActivity;
import com.cgtong.android.bean.VersionBean;
import com.cgtong.android.http.ApiManager;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.util.CommonUtils;
import com.google.gson.Gson;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.umeng.analytics.MobclickAgent;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


/**
 * 设置
 */
public class MySettingActivity extends BaseActivity {

    private static final String TELEPHONE = "0887-73463828";
    private static final int PHONE_REQ = 122;

    @BindView(R.id.title_center_txt)
    TextView mTitleCenterTxt;
    @BindView(R.id.tv_rule)
    TextView tv_rule;
    @BindView(R.id.txt_version)
    TextView txt_version;
    @BindView(R.id.update_notice)
    View mUpdateNotice;

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_setting;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        getVersionNotice();
    }

    private void initView() {
        SpannableString spannableString = new SpannableString(getResources().getString(R.string.title_setting_rule));
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Intent intent = new Intent(MySettingActivity.this, RegisterLicenseActivity.class);
                startActivity(intent);
            }
        }, 0, 4, Spanned.SPAN_MARK_MARK);
//        spannableString.setSpan(new ClickableSpan() {
//            @Override
//            public void onClick(View widget) {
//                doCallPermission();
//            }
//        }, 6, 10, Spanned.SPAN_MARK_MARK);
//        spannableString.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color_txt_num)), 10, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_rule.setText(spannableString);
        tv_rule.setMovementMethod(LinkMovementMethod.getInstance());
        txt_version.setText(getString(R.string.title_setting_version, AppUtils.getAppVersionName()));
    }


    private void doCallPermission() {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(Manifest.permission.CALL_PHONE)
                .subscribe(new Observer<Boolean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Boolean aBoolean) {
                        if (aBoolean) {
                            startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + TELEPHONE)));
                        } else {
                            ToastUtils.showLong(R.string.permission_request_denied);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void initData() {
        mTitleCenterTxt.setText(getString(R.string.txt_me_about));
    }

    @OnClick({R.id.title_left_txt, R.id.tv_intro, R.id.update_layout, R.id.tv_cache, R.id.tv_rule, R.id.tv_pwd/*, R.id.tv_tel*/})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_pwd://修改密码
                startActivity(new Intent(MySettingActivity.this, ModifyPwdActivity.class));
                break;
            case R.id.title_left_txt:
                finish();
                break;
            case R.id.update_layout://版本更新
                CommonUtils.getversion();
                break;
            case R.id.tv_intro://功能介绍
                startActivity(new Intent(MySettingActivity.this, MyIntroActivity.class));
                break;
            case R.id.tv_cache://清除缓存
                cleanCache();
                break;
            default:
                break;
        }
    }

    private void cleanCache() {
        try {
            File glideDir = Glide.getPhotoCacheDir(this);
//                    long glideSize = FileUtils.getDirLength(glideDir);
//                    long uploadPhotoSize = FileUtils.getDirLength(ConstantManager.PHOTO_SAVE_DIR);
//                    long uploadAudioSize = FileUtils.getDirLength(ConstantManager.AUDIO_SAVE_DIR);
//                    String dirSize = (glideSize + uploadPhotoSize + uploadAudioSize) / ConstantManager.MB_1 + "MB";
            FileUtils.deleteAllInDir(ConstantManager.PHOTO_SAVE_DIR);
            FileUtils.deleteAllInDir(ConstantManager.AUDIO_SAVE_DIR);
            FileUtils.deleteAllInDir(getCacheDir());
            FileUtils.deleteAllInDir(glideDir);
//                    ToastUtils.showShort(getString(R.string.txt_clean_over, dirSize));
            ToastUtils.showShort(getString(R.string.txt_clean_over2));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("设置页");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("设置页");
        MobclickAgent.onPause(this);
    }

    private void getVersionNotice(){
        //检测版本更新
        EasyHttp.post(ApiManager.URL_GET_VERSIONS)
                .params("version_mark", "10000")
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {

                    }

                    @Override
                    public void onSuccess(String s) {
                        VersionBean versionBean = new Gson().fromJson(s, VersionBean.class);

                        if (versionBean.getCode() == 0) {
                            VersionBean.Version version = versionBean.getData();
                            String versionStr = version.getVersion();
                            int versionCode = Integer.parseInt(versionStr);//最新版本号

                            int appVersionCode = AppUtils.getAppVersionCode();
                            if (versionCode > appVersionCode) {
                                mUpdateNotice.setVisibility(View.VISIBLE);
                            }
                        } else {
                            mUpdateNotice.setVisibility(View.GONE);
                        }
                    }
                });
    }
}
